package cn.chia.back.action;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;

import cn.chia.pojo.Customerservices;
import cn.chia.service.CSService;
import cn.chia.utils.BuildLogFile;
import cn.chia.utils.Md5Util;
import cn.chia.utils.OnCustomerService;

/*
 * 于辉  后台客服Action
 */
@Controller
public class CSAction {
	private CSService cSService;                //客服Service
	private Customerservices customerservice;   //客服对象
	private String csname;                      //客服名
	private String password;                    //客服密码
	private String verifycode;                  //页面自己输入的验证码
	
	/*
	 * 于辉  客服登陆验证
	 */
	public String login() throws Exception{
		HttpServletRequest request = ServletActionContext.getRequest();
		//验证码验证
		String scode = (String) request.getSession().getAttribute("VerifyCode");
		if(!verifycode.equalsIgnoreCase(scode)){
			request.setAttribute("error2", "验证码错误!!");
			return "error";
		}
		
		customerservice = cSService.findByUserPass(csname,Md5Util.addMd5(password));
		if(customerservice==null){
			request.setAttribute("error","用户名或密码错误!");
			return "error";
		}
		request.getSession().setAttribute("oncustomerservice", customerservice);
		//生成日志
		BuildLogFile.build(OnCustomerService.customerService().getCsname()+"于"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+"登陆本系统。\r\n");
		return "success";
	}
	
	/*
	 * 于辉  客服登出
	 */
	public String logout(){
		//生成日志
		BuildLogFile.build(OnCustomerService.customerService().getCsname()+"于"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+"登出本系统。\r\n");
		ServletActionContext.getRequest().getSession().removeAttribute("oncustomerservice");
		return "success";
	}

	/*
	 * 于辉  以下为各属性的get、set方法
	 */
	public Customerservices getCustomerservice() {
		return customerservice;
	}
	public void setCustomerservice(Customerservices customerservice) {
		this.customerservice = customerservice;
	}
	public String getCsname() {
		return csname;
	}
	public void setCsname(String csname) {
		this.csname = csname;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getVerifycode() {
		return verifycode;
	}



	public void setVerifycode(String verifycode) {
		this.verifycode = verifycode;
	}



	@Resource(name="CSServiceImpl")
	public void setcSService(CSService cSService) {
		this.cSService = cSService;
	}
}
